package cn.com.thinvent.dispatch.service.biz.impl;

import cn.com.thinvent.dispatch.dao.DailyExpensesMapper;
import cn.com.thinvent.dispatch.entity.DailyExpenses;
import cn.com.thinvent.dispatch.entity.response.PagedGridResult;
import cn.com.thinvent.dispatch.service.biz.DailyExpensesService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class DailyExpensesServiceImpl implements DailyExpensesService {

    @Resource
    private DailyExpensesMapper dailyExpensesMapper;

    @Override
    public PagedGridResult<DailyExpenses> getAllDailyExpenses(int page, int pageSize, String expenseItem) {
        int offset = (page - 1) * pageSize;
        List<DailyExpenses> list = dailyExpensesMapper.findDailyExpensesWithPagination(expenseItem, offset, pageSize);
        int totalRecords = dailyExpensesMapper.countDailyExpenses(expenseItem);

        PagedGridResult<DailyExpenses> pagedResult = new PagedGridResult<>();
        pagedResult.setPage(page);
        pagedResult.setTotal((int) Math.ceil((double) totalRecords / pageSize));
        pagedResult.setRecords(totalRecords);
        pagedResult.setRows(list);

        return pagedResult;
    }

    @Override
    public DailyExpenses getDailyExpensesById(String id) {
        return dailyExpensesMapper.selectDailyExpensesById(id);
    }

    @Override
    public void addDailyExpenses(DailyExpenses dailyExpenses) {
        dailyExpensesMapper.insertDailyExpenses(dailyExpenses);
    }

    @Override
    public void updateDailyExpenses(DailyExpenses dailyExpenses) {
        dailyExpensesMapper.updateDailyExpenses(dailyExpenses);
    }

    @Override
    public void deleteDailyExpenses(String id) {
        dailyExpensesMapper.deleteDailyExpenses(id);
    }

}
